var addNum = 0,nowNum = 0,itemHeight = 46,cmItem = $('.cm-item'),cmList = $('.cm-list');
var winHeight = $(window).height();
var seeIndex = parseInt((winHeight - 184) / itemHeight); 
(function(){
	var allIndex = cmItem.length;
	var i = 0,j = 0;
	var flag = true;
	$('.cm-wrapper').height(winHeight + 'px');
	cmList.height(itemHeight + 'px');
	function move(){ 
		itemScale = setInterval(function(){
			//display -> add
			cmItem.eq(i).find('span').removeClass('hide');
			var allItem = allIndex + addNum;
			//remove
			if(i > seeIndex + 4 || cmList.height() >= (seeIndex + 4) * itemHeight){
				if(flag){
					$('.cm-item').eq(j).animate({height:'0px'},600);
					flag = false;
				}else {
					$('.cm-item').eq(j + 1).animate({height:'0px'});
					setTimeout(function(){
						$('.cm-item').eq(j).remove();
					}, 600);
				}
			}else {
				cmList.animate({height:itemHeight * (nowNum + 2) + 'px'});
			}
			i ++;
			nowNum ++;
		}, 1000);
	};
	function stopMove(){
		clearInterval(itemScale);
	}
	move();
})();
// 添加评论
function addCmItem(){
	var cmText = $('#cmText').val();
	if(cmText == ''){
		warn('内容为空','','','');
	}else{
		if(cmList.height() >= (seeIndex + 4) * itemHeight){
			var addPos = cmItem.length - $('.cm-list .hide').length - 1;
			cmList.animate({height:cmList.height() + itemHeight * 2 + 'px'});
			cmItem.eq(addPos).after(function(){
				return '<div class="cm-item"><span class="cm-r tranScale"><img src="./images/photo-img.png" alt="">' + cmText + '</span></div>';
			});
		}else {
			var addPos = cmItem.length - $('.cm-list .hide').length - 1;
			addNum ++;
			cmList.animate({height:itemHeight * (nowNum + 2) + 'px'});
			nowNum ++;
			cmItem.eq(addPos).after(function(){
				return '<div class="cm-item"><span class="cm-r tranScale"><img src="./images/photo-img.png" alt="">' + cmText + '</span></div>';
			});
		}
	}
};
//不打断alert
function warn(msg,opt,left,top){
	if(opt){
	 	var obj = $("#"+opt);
	}
 	new Toast({context:$('body'),message:msg},obj,left,top).show();
}
var Toast = function(config,obj,left,top){
	this.context = config.context==null?$('body'):config.context;//上下文
	this.message = config.message;//显示内容
	this.time = config.time==null?5000:config.time;//持续时间
	this.left = config.left;//距容器左边的距离
	this.top = ($(document).height()/4)*3;//距容器上方的距离
	if(obj){ 
	this.left = obj.offset().left + left;
	this.top = obj.offset().top + top;
	}
	this.init();
}
var msgEntity;
Toast.prototype = {
	//初始化显示的位置内容等
	init : function(){
		$("#toastMessage").remove();
		//设置消息体
		var msgDIV = new Array();
		msgDIV.push('<div id="toastMessage">');
		msgDIV.push('<span>'+this.message+'</span>');
		msgDIV.push('</div>');
		msgEntity = $(msgDIV.join('')).appendTo(this.context);
		//设置消息样式
		var left = this.left == null ? this.context.width()/2-17-msgEntity.find('span').width()/2 : this.left;
		var top = this.top == null ? '20px' : this.top;
		msgEntity.css({position:'absolute',top:top,'z-index':'999',left:left,'background-color':'black',color:'white','font-size':'14px',padding:'5px 12px',margin:'5px','border-radius':'4px','-moz-border-radius':'4px','-webkit-border-radius':'4px',opacity:'0.6','font-family':'微软雅黑'});
		//msgEntity.addClass(".toast");
		msgEntity.hide();
 	},
	//显示动画
	show :function(){
		msgEntity.fadeIn(this.time/2);
		msgEntity.fadeOut(this.time/2);
	}
}
